﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LiveCPEWinForm.Gestionnaire;
using LiveCPEWinForm.forms;
using LiveCPEWinForm.comp;

namespace LiveCPEWinForm
{
    public partial class Master : Form
    {
        GestAPP lGestAPP;
        UserControl lUcEc;

        public Master(GestAPP pGestAPP)
        {
            InitializeComponent();
            lGestAPP = pGestAPP;
        }

        private void Master_Load(object sender, EventArgs e)
        {
            // Activation de l'evenement
            lGestAPP.Changed += new GestAPP.StatusChange(gestAPP_Changed);
            lGestAPP.WrkInProg += new GestAPP.WorkInProgress(lGestAPP_WrkInProg);
            lGestAPP.MessageChanged += new GestAPP.MessageChange(lGestAPP_MessageChanged);
            refreshStatus();
            // Initialisation du timer de scrutation des messages
            lGestAPP.InitTimer();
        }
        // Nouveau Message
        void lGestAPP_MessageChanged(object sender, GestAPP.ChangeMessageEventArgs e)
        {
            if (e.Reload)
                btnWall_Click(this, null);
            else
                btnWall.ForeColor = Color.Red;
        }
        // Evenement sur action en cours
        void lGestAPP_WrkInProg(object sender, EventArgs e)
        {
            // Affichage de l'action en cours
            lblConnectAction.Text = "Work in Progresss...";
            // Mise à jour de la barre
            if (prgBar.Value + 10 > prgBar.Maximum)
                prgBar.Value = 0;
            else
                prgBar.Value += 10;
        }
        // Envenement sur changement d'état
        void gestAPP_Changed(object sender, GestAPP.ChangeEventArgs e)
        {
            refreshStatus();
        }
        // Rafraichissement de l'écran
        private void refreshStatus()
        {
            switch (lGestAPP.ConnectState)
            {
                case 0:
                    lblTitle.Text = "Welcome on our community Project.\n please connect !";
                    lblConnectAction.Text = "Connect";
                    prgBar.Value = 0;
                    // Libération dans panel1 (Liste des control insérer)
                    panel1.Controls.Clear();
                    panMenu.Visible = false;
                    break;
                case 1:
                    lblConnectAction.Text = "Disconnect";
                    panMenu.Visible = true;
                    if (lGestAPP.img != null) pbProfile.Image = lGestAPP.img;
                    prgBar.Value = 100;
                    break;
            }
            if (lGestAPP.user != null)
            {
                lblTitle.Text = "Welcome " + lGestAPP.user.FirstName.TrimEnd() + " " + lGestAPP.user.LastName.TrimEnd() + " (" + lGestAPP.user.UserName.TrimEnd() + ")";
            }
            // Label de status
            lblStatusCommand.Text = lGestAPP.ConnectionStatus;
            
        }

        private void lblConnectAction_Click(object sender, EventArgs e)
        {
            FmLogin fm = new FmLogin(lGestAPP);
            fm.ShowDialog();
            fm.Dispose();
        }

        private void btnCreateUser_Click(object sender, EventArgs e)
        {
            FmCreateUser fm = new FmCreateUser(lGestAPP);
            fm.ShowDialog();
            fm.Dispose();
        }

        private void btnProfil_Click(object sender, EventArgs e)
        {
            panel1.Controls.Clear();
            // Profiles
            lUcEc = new ucProfile(lGestAPP);
            lUcEc.Width = panel1.Width;
            lUcEc.Height = panel1.Height;
            lUcEc.Parent = panel1;
        }

        private void panel1_Resize(object sender, EventArgs e)
        {
            if (lUcEc != null)
            {
                lUcEc.Width = panel1.Width;
                lUcEc.Height = panel1.Height;
                lUcEc.Refresh();
            }
        }

        private void btnWall_Click(object sender, EventArgs e)
        {
            btnWall.ForeColor = btnProfil.ForeColor;
            panel1.Controls.Clear();
            // WALL
            lUcEc = new CsWall(lGestAPP);
            lUcEc.Width = panel1.Width;
            lUcEc.Height = panel1.Height;
            lUcEc.Parent = panel1;
        }

        private void btnFriends_Click(object sender, EventArgs e)
        {
            panel1.Controls.Clear();
            // Profiles
            lUcEc = new CsFriends(lGestAPP);
            lUcEc.Width = panel1.Width;
            lUcEc.Height = panel1.Height;
            lUcEc.Parent = panel1;
        }
    }
}
